<?php
/**
 * Created by PhpStorm.
 * User: xuho
 * Date: 2020-05-04
 * Time: 00:10
 */

namespace app\common\subscribe;


use app\common\model\mysql\AccountOperationModel;
use think\facade\Cache;
use think\facade\Cookie;

class OperaSubscribe
{
    public function onOpera($opera)
    {
        if (empty($opera)) {
            return '';
        }

        $token = Cookie::get('token');

        if (empty(Cache::exists($token))) {
            $token = Cookie::get('shop_token');
            if (empty(Cache::exists($token))) {
                return '';
            }
        }
        $account = Cache::get($token);


        $request = app('request');

        $accountOpera['account_id'] = $account['id'];
        $accountOpera['account_name'] = $account['account_name'];
        $accountOpera['route'] = $request->url();
        $accountOpera['title'] = $opera['title'] ?? '操作行为';
        $accountOpera['content'] = $opera['content'];
        $accountOpera['ip'] = $request->ip();
        $accountOpera['useragent'] = $request->header('user-agent');
        $accountOpera['outcome'] = $opera['outcome'] ?? 'success';
        $accountOpera['system_id'] = config('admin.system_id');
        $accountOpera['store_id'] = $account['store_id'] ?? 0;
        AccountOperationModel::addData($accountOpera);
    }
}